package kr.co.bettersoft.sendme.rest;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;

import kr.co.bettersoft.sendme.provider.SendMeMember;
import kr.co.bettersoft.sendme.util.SendMeConstant;
import android.util.Log;

public class SendMeMemberRest {

	public static String TAG = SendMeMemberRest.class.getSimpleName();
	
	/**
	 * 휴대전화번호로 멤버 중복 여부를 확인한다.
	 * 
	 * @param sendMeMember
	 * @return
	 */
	public SendMeMember selectDuplication(SendMeMember sendMeMember) {
		Log.i(TAG, "selectDuplication");
		
		String url = SendMeConstant.SERVER_URL
			+ SendMeConstant.MAPPING_CONTROLLER
			+ SendMeConstant.MEMBER_CONTROLLER
			+ SendMeConstant.MEMBER_SELECT_DUPLICATION;
		
		SendMeMember result = new SendMeMember();
		
		try {
			JSONObject parameterJsonObject = new JSONObject();
			parameterJsonObject.put("phoneNumber", sendMeMember.getPhoneNumber());
			parameterJsonObject.put("activateYn", sendMeMember.getActivateYn());
			
			JSONObject resultJsonObject = new JSONObject(this.requestToServer(url, parameterJsonObject.toString()));
			result.setTotalCount(resultJsonObject.getString("totalCount"));
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	/**
	 * 멤버를 등록 한다.
	 * 
	 * @param sendMeMember
	 * @return
	 */
	public SendMeMember registerMember(SendMeMember sendMeMember) {
		Log.i(TAG, "registerMember");
		
		String url = SendMeConstant.SERVER_URL
			+ SendMeConstant.MAPPING_CONTROLLER
			+ SendMeConstant.MEMBER_CONTROLLER
			+ SendMeConstant.REGISTER_MEMBER;
		
		SendMeMember result = new SendMeMember();
		
		try {
			JSONObject parameterJsonObject = new JSONObject();
			parameterJsonObject.put("phoneNumber", sendMeMember.getPhoneNumber());
			parameterJsonObject.put("nickName", sendMeMember.getNickName());
			parameterJsonObject.put("email", sendMeMember.getEmail());
			parameterJsonObject.put("deviceType", sendMeMember.getDeviceType());
			parameterJsonObject.put("pushId", sendMeMember.getPushId());
			parameterJsonObject.put("payYn", sendMeMember.getPayYn());
			parameterJsonObject.put("activateYn", sendMeMember.getActivateYn());
			parameterJsonObject.put("modifyDate", sendMeMember.getModifyDate());
			parameterJsonObject.put("registerDate", sendMeMember.getRegisterDate());
			
			JSONObject resultJsonObject = new JSONObject(this.requestToServer(url, parameterJsonObject.toString()));
			result.setResult(resultJsonObject.getString("result"));
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	
	/**
	 * 멤버 정보를 가져 온다.
	 * 
	 * @param sendMeMember
	 * @return
	 */
	public SendMeMember selectMember(SendMeMember sendMeMember) {
		Log.i(TAG, "registerMember");

		String url = SendMeConstant.SERVER_URL
			+ SendMeConstant.MAPPING_CONTROLLER
			+ SendMeConstant.MEMBER_CONTROLLER
			+ SendMeConstant.SELECT_MEMBER;
		
		SendMeMember result = new SendMeMember();
		
		try {
			JSONObject parameterJsonObject = new JSONObject();
			parameterJsonObject.put("phoneNumber", sendMeMember.getPhoneNumber());
			parameterJsonObject.put("activateYn", sendMeMember.getActivateYn());
			String resultStr = this.requestToServer(url, parameterJsonObject.toString());
//			Log.d(TAG,"resultStr:"+resultStr);

			JSONObject resultJsonObject = new JSONObject(resultStr);
			/*
			 * {"phoneNumber":"01088418653",
			 * "nickName":"",
			 * "email":"",
			 * "deviceType":"AS",
			 * "pushId":"APA91bFIt12ru9fw9J_-uJ5xVF1OwIslyK-tyhoFC8luyQdkfGegsFV_FbTdY
			 * "payYn":"N",
			 * "activateYn":"Y",
			 * "modifyDate":"2014-02-13 19:04:23",
			 * "registerDate":"2014-02-13 19:04:23",
			 */
			result.setPhoneNumber(resultJsonObject.getString("phoneNumber"));
			result.setNickName(resultJsonObject.getString("nickName"));
			result.setEmail(resultJsonObject.getString("email"));
			result.setDeviceType(resultJsonObject.getString("deviceType"));
			result.setPushId(resultJsonObject.getString("pushId"));
			result.setPayYn(resultJsonObject.getString("payYn"));
			result.setRegisterDate(resultJsonObject.getString("registerDate"));
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	/**
	 * 멤버 닉네임 중복검사를 한다.			
	 * 
	 * @param sendMeMember
	 * @return
	 */
	public SendMeMember selectDuplicationNickName(SendMeMember sendMeMember) {
		Log.i(TAG, "selectDuplicationNickName");
		
		String url = SendMeConstant.SERVER_URL
			+ SendMeConstant.MAPPING_CONTROLLER
			+ SendMeConstant.MEMBER_CONTROLLER
			+ SendMeConstant.SELECT_DUPLICATION_NICK_NAME;
		
		SendMeMember result = new SendMeMember();
		
		try {
			JSONObject parameterJsonObject = new JSONObject();
			parameterJsonObject.put("phoneNumber", sendMeMember.getPhoneNumber());
			parameterJsonObject.put("nickName", sendMeMember.getNickName());
			parameterJsonObject.put("activateYn", sendMeMember.getActivateYn());
			String resultStr = this.requestToServer(url, parameterJsonObject.toString());
//			Log.d(TAG,"resultStr:"+resultStr);
			JSONObject resultJsonObject = new JSONObject(resultStr);
			result.setTotalCount(resultJsonObject.getString("totalCount"));	
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	
	
	/**
	 * 멤버 정보를 수정 한다.
	 * 
	 * @param sendMeMember
	 * @return
	 */
	public SendMeMember updateMember(SendMeMember sendMeMember) {
		Log.i(TAG, "updateMember");
		
		String url = SendMeConstant.SERVER_URL
			+ SendMeConstant.MAPPING_CONTROLLER
			+ SendMeConstant.MEMBER_CONTROLLER
			+ SendMeConstant.UPDATE_MEMBER;
		
		SendMeMember result = new SendMeMember();
		
		try {
			
			JSONObject parameterJsonObject = new JSONObject();
			parameterJsonObject.put("phoneNumber", sendMeMember.getPhoneNumber());
			parameterJsonObject.put("nickName", sendMeMember.getNickName());
			parameterJsonObject.put("email", sendMeMember.getEmail());
			parameterJsonObject.put("deviceType", sendMeMember.getDeviceType());
			parameterJsonObject.put("pushId", sendMeMember.getPushId());
			parameterJsonObject.put("payYn", sendMeMember.getPayYn());
			parameterJsonObject.put("activateYn", sendMeMember.getActivateYn());
			parameterJsonObject.put("modifyDate", sendMeMember.getModifyDate());
			String resultStr = this.requestToServer(url, parameterJsonObject.toString());
//			Log.d(TAG,"resultStr:"+resultStr);
			JSONObject resultJsonObject = new JSONObject(resultStr);
			result.setResult(resultJsonObject.getString("result"));
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	
	
	
	
	
	
	/**
	 * push id 를 업데이트 한다.
	 * 
	 * @param sendMeMember
	 * @return
	 */
	public SendMeMember updatePushId(SendMeMember sendMeMember) {
		Log.i(TAG, "updatePushId");
		
		String url = SendMeConstant.SERVER_URL
			+ SendMeConstant.MAPPING_CONTROLLER
			+ SendMeConstant.MEMBER_CONTROLLER
			+ SendMeConstant.UPDATE_PUSH_ID;
		
		SendMeMember result = new SendMeMember();
		
		try {
			JSONObject parameterJsonObject = new JSONObject();
			parameterJsonObject.put("phoneNumber", sendMeMember.getPhoneNumber());
			parameterJsonObject.put("pushId", sendMeMember.getPushId());
			parameterJsonObject.put("activateYn", sendMeMember.getActivateYn());
			parameterJsonObject.put("modifyDate", sendMeMember.getModifyDate());
			String resultStr = this.requestToServer(url, parameterJsonObject.toString());
			
			JSONObject resultJsonObject = new JSONObject(resultStr);
			result.setResult(resultJsonObject.getString("result"));
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	
	/**
	 * http protocal 을 사용하는 서버 요청 공통 메소드.
	 * 요청 후 결과를 json string 으로 받아서 반환 한다.
	 * 
	 * @param url
	 * @param inputJsonString
	 * @return
	 */
	public String requestToServer(String url, String inputJsonString) {
		Log.i(TAG, "requestToServer");
		
		String result = "";
		
		try {
			URL postUrl = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
	        connection.setDoOutput(true);
	        connection.setInstanceFollowRedirects(false);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("Content-Type", "application/json");
	        OutputStream os = connection.getOutputStream();
	        os.write(inputJsonString.getBytes());
	        os.flush();
	        
	        if(connection != null) {
	        	connection.setConnectTimeout(3000);
	        	
	        	if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	        		result = bufferedReader.readLine();
	        	}
	        }
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
}
